// 控制用户权限
import router from '@/router'
import store from '@/store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
// console.log(store.getters.token)

const whiteList = ['/404', '/login']
// 全局前置守卫
router.beforeEach(async(to, from, next) => {
  NProgress.start()
  if (store.getters.token) {
    // 有token
    // 获取用户资料
    // 判断制请求一次数据
    if (!store.getters.userInfo.id) {
      await store.dispatch('user/getUserInfo')
    }
    if (to.path === '/login') {
      // 进主页
      next('/')
    } else {
      next()
    }
  } else {
    // 无token
    if (whiteList.indexOf(to.path) === -1) {
      next('/login')
    } else {
      next()
    }
  }
  NProgress.done()
})

router.afterEach(() => {
  NProgress.done()
})
